const express = require('express');
// 创建一个路由
const router = express.Router();
const mysql = require('../../conn');
const getDate = require('../../utils');

// 商品查询
router.get('/get', (req, res) => {
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的查询语句
    let sql = `select * from commodity`;

    mysql.query(sql, (err, results) => {
        if (err) return console.log(err);
        obj.code = 200;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);
    });
});

// id查询商品
router.get('/info', (req, res) => {
    let { commodity_id } = req.query;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的查询语句
    let sql = `select * from commodity where commodity_id = ?`;

    mysql.query(sql, [commodity_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 200;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);
    });
});

// 增加商品
router.post('/add', (req, res) => {
    let { commodity_title, describe, commodity_image, price, lastprice, sell, addr } = req.body;
    let create_time = getDate();
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `insert into commodity (commodity_title,\`describe\`,commodity_image, price, lastprice, sell, addr,create_time) value (?,?,?,?,?,?,?,?)`;

    mysql.query(sql, [commodity_title, describe, commodity_image, price, lastprice, sell, addr, create_time], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);

    });
});

// 删除商品
router.put('/delete', (req, res) => {
    let { commodity_id } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `delete from commodity where commodity_id in (?)`;

    mysql.query(sql, [commodity_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        // obj.data = results;
        res.send(obj);
    });
});

// 修改商品
router.put('/update', (req, res) => {
    let { commodity_title, describe, commodity_image, price, lastprice, sell, addr, commodity_id } = req.body;
    let create_time = getDate();
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `update commodity set commodity_title=?, \`describe\`=?, commodity_image=?, price=?, lastprice=?, sell=?, addr=? , create_time=? where commodity_id in (?);`;

    mysql.query(sql, [commodity_title, describe, commodity_image, price, lastprice, sell, addr, create_time, commodity_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);
    });
});

module.exports = router;